package view
{
	
	import flash.display.*;
	import flash.text.*;
	
	import model.ExplorePx;
	import manager.ViewMgr;
	import event.Evt;
	
	/**
	 * -- StageListView --
	 * @desc	view of stage list
	 * @author	Aqua (built on 2011-11-23)
	 */
	public class StageListView extends BaseView
	{
		//---------------------//
		//     Constructor     //
		//---------------------//
		
		/**************************************************************************
		 * -- StageListView --
		 * @desc		constructor
		 * @param		main view object of this view class
		 *************************************************************************/
		public function StageListView(viewComponent:DisplayObjectContainer):void
		{
			super(viewComponent);
			
			_viewComponent.name = VIEW_NAME;
			_explorePx = getPx(ExplorePx.NAME) as ExplorePx;
			_currentPage = 1;
			
			addAddCmd("txt_name",		add_txt_name);
			addAddCmd("txt_level",		add_txt_level);
			addAddCmd("btn_stage",		add_btn);
			addAddCmd("image_stage",	add_image_stage);
			addAddCmd("txt_page",		add_txt_page);
			addAddCmd("btn_left",		add_btn);
			addAddCmd("btn_right",		add_btn);
			
			addClickCmd("btn_stage",	click_btn_stage);
			addClickCmd("btn_left",		click_btn_left);
			addClickCmd("btn_right",	click_btn_right);
		}
		
		
		//------------------//
		//     Privates     //
		//------------------//
		
		//-- Add Handlers --//
		private function add_txt_name():void
		{
			var index:int;
			var xml:XML;
			
			index = getIndex(_display) + NUM_PER_PAGE * (_currentPage - 1);
			xml = _explorePx.getStageXML(index);
			
			if (xml)
			{
				TextField(_display).text = xml.name;
			}
			else
			{
				_display.parent.visible = false;
			}
		}
		private function add_txt_level():void
		{
			var index:int;
			var fromLv:String;
			var toLv:String;
			var xml:XML;
			
			index = getIndex(_display) + NUM_PER_PAGE * (_currentPage - 1);
			xml = _explorePx.getStageXML(index);
			
			if (xml)
			{
				fromLv = xml.level_from;
				toLv = xml.level_to;
				TextField(_display).text = "(Lv " + fromLv + "-" + toLv + ")";
			}
		}
		private function add_btn():void
		{
			makeBtn(_display);
		}
		private function add_image_stage():void
		{
			var index:int;
			var xml:XML;
			
			index = getIndex(_display) + NUM_PER_PAGE * (_currentPage - 1);
			xml = _explorePx.getStageXML(index);
		}
		private function add_txt_page():void
		{
			TextField(_display).text = String(_currentPage) + "/" + String(int(_explorePx.stageNum / NUM_PER_PAGE) + 1);
		}
		
		//-- Click Handlers --//
		private function click_btn_stage():void
		{
			var index:int;
			
			index = getIndex(_display) + NUM_PER_PAGE * (_currentPage - 1);
			_explorePx.chooseStage(index);
			_evt.dispatch(Evt.CHOOSE_STAGE);
		}
		private function click_btn_left():void
		{
			if (_currentPage > 1)
			{
				_currentPage --;
				onRefresh();
			}
		}
		private function click_btn_right():void
		{
			if (_currentPage < _explorePx.stageNum)
			{
				_currentPage ++;
				onRefresh();
			}
		}
				
		
		public static const VIEW_NAME	:String = "STAGE_LIST_VIEW";
		public static const LAYER_INDEX	:int	= 2;
		
		private const NUM_PER_PAGE	:int = 9;
		
		private var _explorePx		:ExplorePx;
		private var _currentPage	:int;
	}
	
}